/**
 * 
 */
package de.cbf.cam.dsa.facet.armor.helper.editing_support;

import java.text.NumberFormat;
import java.text.ParseException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;

import de.cbf.cam.dsa.facet.armor.helper.ZoneFactors;
import de.cbf.util.ui.helper.ICustomizableEditingSupport;

/**
 * @author salter
 *
 */
public class EditingSupportZoneFactors extends EditingSupport implements ICustomizableEditingSupport{

private static final Log logger = LogFactory.getLog(EditingSupportZoneFactors.class);
	
	protected NumberFormat format;

	protected TableViewer tableViewer;

	protected String zoneName;
	/**
	 * @param viewer
	 */
	public EditingSupportZoneFactors(ColumnViewer viewer) {
		super(viewer);		
		format = NumberFormat.getNumberInstance();
		if (viewer instanceof TableViewer) {
			tableViewer = (TableViewer)viewer;			
		}else{
			String err_msg = "entity of wrong type in EditingSupportZoneFactors() : \n"+
					"Expected: "+TableViewer.class.getSimpleName()+"\n"+ 
					"Got: "+ (viewer == null ? "null" : viewer.getClass().getSimpleName());
			logger.warn(err_msg);
		}		
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.EditingSupport#getCellEditor(java.lang.Object)
	 */
	@Override
	protected CellEditor getCellEditor(Object element) {		
		return new TextCellEditor(tableViewer.getTable());
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.EditingSupport#canEdit(java.lang.Object)
	 */
	@Override
	protected boolean canEdit(Object element) {
		return true;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.EditingSupport#getValue(java.lang.Object)
	 */
	@Override
	protected Object getValue(Object element) {
		if (element instanceof ZoneFactors) {
			ZoneFactors zoneFactors = (ZoneFactors)element;
			Double value = zoneFactors.getZoneFactor(zoneName);
			String valueString = format.format(value);
			return valueString;
		}else{
			String err_msg = "entity of wrong type in getValue : \n"+
					"Expected: "+ZoneFactors.class.getSimpleName()+"\n"+ 
					"Got: "+ (element == null ? "null" : element.getClass().getSimpleName());
			logger.error(err_msg);
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.EditingSupport#setValue(java.lang.Object, java.lang.Object)
	 */
	@Override
	protected void setValue(Object element, Object value) {
		if (element instanceof ZoneFactors && value instanceof String) {
			ZoneFactors zoneFactors = (ZoneFactors)element;
			String zoneFactorString = (String)value;
			Number number;
			try {
				number = format.parse(zoneFactorString);
				Double result = 0D;
				if (number instanceof Long) {
					Long numberLong = (Long)number;
					result = numberLong.doubleValue();
					zoneFactors.replaceZoneFactor(zoneName,result);									
				}
				if (number instanceof Double) {
					result = (Double)number;					
					zoneFactors.replaceZoneFactor(zoneName,result);									
				}				
				tableViewer.update(element, null);
			} catch (ParseException e) {
				logger.error(e);
			}			
		}else{
			String err_msg = "entity of wrong type in setValue : \n"+
					"Expected: "+ZoneFactors.class.getSimpleName()+"\n"+ 
					"Got: "+ (element == null ? "null" : element.getClass().getSimpleName());
			logger.error(err_msg);
		};
	}

	/**
	 * @return the zoneName
	 */
	public String getZoneName() {
		return zoneName;
	}

	/**
	 * @param zoneName the zoneName to set
	 */
	public void setZoneName(String zoneName) {
		this.zoneName = zoneName;
	}

	@Override
	public void customize(Object obj) {
		if (obj instanceof String) {
			zoneName = (String) obj;			
		} else {
			String err_msg = "entity of wrong type in customize : \n"
					+ "Expected: " + String.class.getSimpleName() + "\n"
					+ "Got: "
					+ (obj == null ? "null" : obj.getClass().getSimpleName());
			logger.error(err_msg);
		}		
	}
}
